package edu.unc.comp410.colemangeer.demos;

import java.util.*;
import org.apache.log4j.Logger;
import edu.unc.comp410.colemangeer.model.*;
import edu.unc.comp410.colemangeer.ui.BrowseGeneralGUI;

public class ClerkOrderDemo {
	
	private Order order;
	private static Logger log = Logger.getLogger(ClerkOrderDemo.class);
	//Hashtable<Integer,Integer> prodHT_Q; //holds prodId's and their quantities
	Hashtable<Integer,Product> prodHT; //holds Inventory Indexes and their products
	Set<Product> prodSet; 
	Integer idx;
	
	public static void main( String args[] ){
		ClerkOrderDemo demo = new ClerkOrderDemo();
		//make a fake order to work with
		demo.initOrder();

		//open up the order and show the products within it
		demo.reviewOrder();
		
		//allow the clerk to assign a warehouse to each product
		log.warn("Clerk now choosing warehouses... chooseWarehouseLoop()" );
		demo.chooseWarehouseLoop_2(  );
		
		//if warehouse is out, then allow clerk to do something else
		
		//allow clerk to reassign to a different warehouse
		log.warn("Warehouse sold out, choose another... changeWarehouse()" );
		demo.changeWarehouse( 1 );
		
	}
	
	/**
	 * @ensures that a 
	 */
	public void reviewOrder(){
		//show the details
		log.warn("Currently reviewing Order #" + getOrder().getId() + " from " + getOrder().getCustomer().getName() );
		
		//show each product
		Set<LineItem> liSet = getOrder().getItems().getAllItems();
		//loop through all lineItems to get each catItem and its quantity
		prodSet = new HashSet<Product>();
		//prodHT_Q = new Hashtable<Integer,Integer>(); //prodID, quantity
		prodHT = new Hashtable<Integer,Product>(); //inventory index, product
		idx = 0;
		for( LineItem li : liSet ){
			Integer currentQuantity = li.getQuantity();
			CatalogItem currentCatItem = li.getCatalogItem();
			//log.warn( "Catalog Item ordered: " + currentCatItem.getName() + " Quantity: " + currentQuantity );
			List<Product> currentProdList = currentCatItem.getAllProducts();
			//idx = 0;
				//for( int i=0; i<currentQuantity; i++ ){
					for( Product p : currentProdList ){
						//System.out.println("hello: *" + idx + "*");
						prodSet.add(p);
						//prodHT.put(idx, p);
						
						//multiple section
						for( int i=0; i<currentQuantity; i++ ){
							if( i==0 ){
								prodHT.put(idx, p);
								idx++;
							}
							else{
									//Product pNew = p.clone();
									//public Product(Integer id, String productName, Double price, List<ProductCategory> categories)
									Product pNew = new Product( p.getId(), p.getName(), p.getPrice(), p.getConstructCat() );
									prodHT.put(idx, pNew);
									idx++;
							}
							
							
						}
						//end multiple section
						
							//sum section
							/*if( prodHT.containsKey(p.getId()) ){ //then replace the value with the sum
								Integer prevQuantity = prodHT_Q.get(p.getId());
								prodHT.remove(p.getId());
								Integer newQuantity = 
									prevQuantity + 
								currentQuantity;
								prodHT_Q.put( p.getId(), newQuantity );
							}
							else{
								prodHT_Q.put( p.getId(), currentQuantity );
							}*/
							log.warn( "\tProducts ordered: \"" + p.getName() + "\", Quantity: " + currentQuantity );
							//end sum section
						//idx++;
					}
				//}
			//idx++;
		}
		//log.warn("Product #" + getOrder().getId() +
	}
	
	public void chooseWarehouseLoop(  ){
		//loop through the prodSet and call chooseWarehouse for each 
		for( Product ps : prodSet ){
			ps.setWarehouse(getRandomInt());
			log.warn( "product " + ps.getName() + " assigned to Warehouse #" + ps.getWarehouse() );
		}
	}
	
	public void chooseWarehouseLoop_2(  ){
		//loop through the prodHT and call chooseWarehouse for each
		for( int i=0; i<idx; i++ ){
			//System.out.println("current idx is: *" + idx + "*");
			Product curProd = prodHT.get(i);
			curProd.setWarehouse(getRandomInt());
			log.warn( "Product \"" + curProd.getName() + "\" assigned to Warehouse #" + curProd.getWarehouse() );
		}
	}
	
	public void changeWarehouse( Integer theidx ){
		prodHT.get(theidx).setWarehouse(getRandomInt());
		log.warn( "product " + prodHT.get(theidx).getName() + " assigned to Warehouse #" + prodHT.get(theidx).getWarehouse() );
	}
	
	public Integer getRandomInt(){
		Integer result = null;
		Random r = new Random();
		result = Math.abs(r.nextInt()) % 11;
		return result;
	}
	
	public void initOrder(){
		Customer customer = new Customer( 1, "bob smith", "72 crystal oaks ct.", "Durham", "FL", "27707", 919, "357-4864" );
		OrderItems orderItems = new OrderItems();
		//public LineItem(CatalogItem item, Integer quantity){
		//CatalogItem(Integer Id, String name, Double price,List<ISellable> detail, Set<ProductCategory> categories)
		List<ISellable> detail = new ArrayList<ISellable>();
		//4|Harp Tuner|37.50|Misc|
		List<ProductCategory> categories = new ArrayList<ProductCategory>();
		categories.add(ProductCategory.getCategoryByName("Misc"));
		Product p = new Product( 4, "Harp Tuner", new Double(37.50), categories);
		//public Product(Integer id, String productName, Double price, List<ProductCategory> categories)
		detail.add(p);
		CatalogItem catItem = new CatalogItem( p.getId(),p.getName(),p.getPrice(), detail, p.getProductCategories());
		LineItem ItemOne = new LineItem( catItem, 1); //catalogItem, quantity
		
		orderItems.addLineItem( ItemOne );
		PaymentAuthorization paymentAuth = new CreditAccountAuthorization();
		ShippingInfo shipping = new ShippingInfo( "72 crystal oaks ct.", "Durham", "FL", "USA", "27707");
		
		
		
		List<ISellable> detail2 = new ArrayList<ISellable>();
		List<ProductCategory> categories2 = new ArrayList<ProductCategory>();
		categories.add(ProductCategory.getCategoryByName("Misc"));
		Product p2 = new Product( 2, "Laser Flyzapper", new Double(179.99), categories);
		detail2.add(p2);
		CatalogItem catItem2 = new CatalogItem( p2.getId(),p2.getName(),p2.getPrice(), detail2, p2.getProductCategories());
		LineItem ItemTwo = new LineItem( catItem2, 3); //catalogItem, quantity
		
		orderItems.addLineItem( ItemTwo );
		
		/*List<ISellable> detail3 = new ArrayList<ISellable>();
		List<ProductCategory> categories3 = new ArrayList<ProductCategory>();
		categories3.add(ProductCategory.getCategoryByName("Misc"));
		Product p3 = new Product( 2, "Laser Flyzapper", new Double(179.99), categories3);
		detail3.add(p3);
		CatalogItem catItem3 = new CatalogItem( p3.getId(),p3.getName(),p3.getPrice(), detail3, p3.getProductCategories());
		LineItem ItemThree = new LineItem( catItem3, 1); //catalogItem, quantity
		
		orderItems.addLineItem( ItemThree );*/
				
		
		//public Order(Integer id, Customer customer, OrderItems items,PaymentAuthorization paymentAuth, ShippingInfo shipping, double price)
		order = new Order( 23, customer, orderItems, paymentAuth, shipping, new Double(100.00));
	}
	
	public Order getOrder(){
		return order;
	}
	
}
